﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace WcfContrib.Configuration
{
    /// <summary>
    /// Represents a configuration element collection
    /// </summary>
    /// <typeparam name="T">The element type</typeparam>
    public abstract class ConfigurationElementCollection<T> : ConfigurationElementCollection
        where T : ConfigurationElement, new()
    {
        /// <summary>
        /// When overridden in a derived class, creates a new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </summary>
        /// <returns>
        /// A new <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        /// <summary>
        /// Gets the element key for a specified configuration element when overridden in a derived class.
        /// </summary>
        /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to return the key for.</param>
        /// <returns>
        /// An <see cref="T:System.Object"/> that acts as the key for the specified <see cref="T:System.Configuration.ConfigurationElement"/>.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return GetElementKey((T)element);
        }

        /// <summary>
        /// Gets the element key.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The element key</returns>
        protected abstract object GetElementKey(T element);

        /// <summary>
        /// Gets or sets the element at the specified index.
        /// </summary>
        /// <value></value>
        public T this[int index]
        {
            get { return (T)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        /// <summary>
        /// Gets the element with the specified name.
        /// </summary>
        /// <value></value>
        public new T this[string name]
        {
            get
            {
                return (T)BaseGet(name);
            }
        }

        /// <summary>
        /// Gets the index of the element.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>The index of the element</returns>
        public int IndexOf(T element)
        {
            return BaseIndexOf(element);
        }

        /// <summary>
        /// Adds the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void Add(T element)
        {
            BaseAdd(element);
        }

        /// <summary>
        /// Adds a configuration element to the <see cref="T:System.Configuration.ConfigurationElementCollection"/>.
        /// </summary>
        /// <param name="element">The <see cref="T:System.Configuration.ConfigurationElement"/> to add.</param>
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        /// <summary>
        /// Removes the specified element.
        /// </summary>
        /// <param name="element">The element.</param>
        public void Remove(T element)
        {
            if (BaseIndexOf(element) >= 0)
            {
                BaseRemove(GetElementKey(element));
            }
        }

        /// <summary>
        /// Removes the element at the specified index.
        /// </summary>
        /// <param name="index">The index.</param>
        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        /// <summary>
        /// Removes the element with the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        public void Remove(object key)
        {
            BaseRemove(key);
        }

        /// <summary>
        /// Clears the elements collection.
        /// </summary>
        public void Clear()
        {
            BaseClear();
        }
    }
}
